package service;

import entity.Reader;
import entity.ReaderType;
import function.Druid;
import function.ReaderDaoImpl;
import function.ReaderTypeDaoImpl;
import interfaces.ReaderServiceDao;
import tool.MyTool;
import tool.TSUtility;
import tool.YouTool;
import view.LogView;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class ReaderServiceDaoImpl implements ReaderServiceDao {
    ReaderTypeServiceDaoImpl readerTypeServiceDao = new ReaderTypeServiceDaoImpl();
    ReaderTypeDaoImpl readerTypeDao = ReaderTypeDaoImpl.getReaderTypeDao();
    ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
    Scanner scanner = new Scanner(System.in);
    @Override
    public void insert() {

        Connection connection = null;
        try {
            connection = Druid.getConnection();
            String readerId = YouTool.readerId();
            System.out.println("--------目前所拥有的读者类型---------");
            readerTypeServiceDao.getAll();
            System.out.println("----------------------------------");
            GetCount();
            System.out.println("输入拥有的读者类型编号");
            int type = TSUtility.readInt();
            ReaderType readerType = readerTypeServiceDao.updateReturn(type);
            //输入的读者类型是否存在
            if (readerType == null) {
                System.out.println("没有该读者类型,重新选择");
                return;
            }
            System.out.println("输入读者姓名");
            String name = TSUtility.readKeyBoard(5, false);
            System.out.println("输入读者年龄");
            int age = TSUtility.readInt();
            String sex = YouTool.judgmentSex();
            //性别是否正确
            String phone = YouTool.judgmentPhone();
            System.out.println("输入所在系部");
            String dept = TSUtility.readKeyBoard(10, false);
            Date regdate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = sdf.format(regdate);
            Date parse = sdf.parse(s);
            readerDao.insert(connection, new Reader(readerId, type, name, age, sex, phone, dept, parse));
            Journal.record(LogView.getOperator().getName(),"添加了读者",name);
            System.out.println("--------------------------------------------");
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteById() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        System.out.println("--------已拥有的读者类型---------");
        getAll();
        System.out.println("-------------------------------");
        try {
            Connection connection = Druid.getConnection();
            System.out.println("输入你要删除的读者");
            String id = TSUtility.readKeyBoard(8, false);
            Reader reader = judgmentExists(id);
            //输入的读者类型是否存在
            if (reader == null) {
                System.out.println("没有该读者类型,请前往添加");
                return;
            }


           if(reader!=null)
            Journal.record(LogView.getOperator().getName(),"删除了读者id为",reader.getName());
            readerDao.deleteById(connection, id);
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void update() {
        ReaderTypeServiceDaoImpl readerTypeServiceDao = new ReaderTypeServiceDaoImpl();
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        try {
            Connection connection = Druid.getConnection();
            System.out.println("--------已拥有的读者类型---------");
            getAll();
            System.out.println("-------------------------------");
            System.out.println("输入你要修改的编号");
            String id = TSUtility.readString(8, null);
            Reader reader = judgmentExists(id);
            if (reader == null) {
                System.out.println("没有此类型读者，修改失败，先前往添加");
                return;
            }
            //boolean flag = true;
            //while (true) {
            //123123
            System.out.println("辅助功能，帮助查看");
            System.out.println("--------已拥有的读者类型---------");
            getAll();
            System.out.println("-------------------------------");
            //123123

            System.out.println("输入你要修改的读者类型" + reader.getType());
            int type = Integer.parseInt(TSUtility.readString(2, reader.getType() + ""));
            int returnID = type;
            ReaderType readerType = readerTypeServiceDao.updateReturn(returnID);
            if (readerType == null) {
                System.out.println("没有该读者类型,重新输入");
                return;
            }
            System.out.println("输入你要修改的读者名字——" + reader.getName());
            String name = TSUtility.readString(4, reader.getName());
            System.out.println("输入你要修改的读者的年龄——" + reader.getAge());
            int age = Integer.parseInt(TSUtility.readString(2, reader.getAge() + ""));
            //System.out.println("输入你要修改的读者性别" + reader.getSex());
            System.out.println("输入你要修改的读者手机——" + reader.getPhone());
            String phone = YouTool.judgmentPhone(reader.getPhone());
            System.out.println("输入你要修改的读者所在系部——" + reader.getDept());
            String dept = TSUtility.readString(5, reader.getDept());
            reader.setType(type);
            reader.setSex(reader.getSex());
            reader.setName(name);
            reader.setPhone(phone);
            reader.setDept(dept);
            reader.setAge(age);
            reader.setRegdate(reader.getRegdate());
            readerDao.update(connection, reader);
            Journal.record(LogView.getOperator().getName(),"修改了读者",name);
            System.out.println("修改成功");
            Druid.colseResource(connection, null, null);
            //flag=false;
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void getReaderById() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        try {
            Connection connection = Druid.getConnection();
            System.out.println("辅助功能");
            getAll();
            System.out.println("------------------------");
            System.out.println("输入读者的编号");
            String readerId = TSUtility.readKeyBoard(8, false);

            Reader reader = judgmentExists(readerId);
            //输入的读者类型是否存在
            if (reader == null) {
                System.out.println("没有该读者类型,请前往添加");
                System.out.println("-------------------------");
                return;
            }
            Reader readerById = readerDao.getReaderById(connection, readerId);
            System.out.println(readerById);
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getReaderByType() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        try {
            Connection connection = Druid.getConnection();
            System.out.println("辅助功能");
            getAll();
            System.out.println("------------------------");
            System.out.println("输入读者类型的编号");
            int type = TSUtility.readInt();

            Reader reader = judgmentExists(type);
            //输入的读者类型是否存在
            if (reader == null) {
                System.out.println("没有该读者类型,请前往添加");
                System.out.println("-------------------------");
                return;
            }
            Reader readerById = readerDao.getReaderByType(connection, type);
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void getAll() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        try {
            Connection connection = Druid.getConnection();
            List<Reader> all = readerDao.getAll(connection);
            all.forEach(System.out::println);
            Druid.colseResource(connection, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Long GetCount() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Long count = readerDao.getCount(connection);
        return count;
    }

    public Reader judgmentExists(String id) {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Reader reader = readerDao.getReaderById(connection, id);
        Druid.colseResource(connection, null, null);
        return reader;
    }

    public Reader judgmentExists(int id) {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Reader reader = readerDao.getReaderByType(connection, id);
        Druid.colseResource(connection, null, null);
        return reader;
    }

    public void viewReaders() {

        boolean FALG = true;
        while (FALG) {
            System.out.println("查看读者信息");
            System.out.println("-----------------------");
            System.out.println("1.根据读者编号查看");
            System.out.println("2.根据读者类型查看");
            System.out.println("3.查看全部读者类型");
            System.out.println("4.退出");
            System.out.println("-----------------------");
            char c = TSUtility.readMenuSelection();
            switch (c) {
                case '1':
                    if (judgment() .size()==0) {
                        System.out.println("暂无读者，请先添加");
                    } else {
                        getReaderById();
                        break;
                    }

                case '2':

                    if (judgment() .size()==0) {
                        System.out.println("暂无读者，请先添加");
                    } else {
                        getReaderByType();
                        break;
                    }
                case '3':
                    if (judgment() .size()==0) {
                        System.out.println("暂无读者，请先添加");
                    } else {
                        getAll();
                        break;
                    }
                case '4':
                    FALG = false;
            }
        }
    }

    public List judgment() {
        ReaderDaoImpl readerDao = ReaderDaoImpl.getReaderDao();
        Connection connection = null;
        try {
            connection = Druid.getConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Reader> all = readerDao.getAll(connection);

        Druid.colseResource(connection, null, null);
        return all;
    }


public void upGrade(String readerId) throws Exception {
        Connection connection=Druid.getConnection();
        System.out.println("请选择你要升级成的读者类型：");
        Reader reader = readerDao.getReaderById(connection, readerId);
        int typeId =reader.getType();
        ReaderType readerType = readerTypeDao.getReaderTypeById(connection, typeId);
        ArrayList<ReaderType> readerTypes = readerTypeServiceDao.judgmentReaderType(readerType);
        MyTool.printArr(readerTypes);
        Integer index = MyTool.getTypeIndex(readerTypes.size());
        ReaderType readerType1 = readerTypes.get(index);
        reader.setType(readerType1.getId());
        readerDao.update(connection,reader);
        System.out.println("提高用户等级成功");
        Druid.colseResource(connection,null,null);
}

}










